home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Text / Sources / TextFrame.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  12.0 KB  |  462 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                            TextFrame.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                        Anthone Burbidge
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. // ----- Macintosh Includes -----
  14.  
  15. #ifndef __FIXMATH__
  16. #include <FixMath.h>
  17. #endif
  18.  
  19. #ifndef mathRoutinesIncludes
  20. #include <Math Routines.h>
  21. #endif
  22.  
  23. #ifndef FWUTIL_H
  24. #include <FWUtil.h>
  25. #endif
  26.  
  27. #ifndef FWRECT_H
  28. #include <FWRect.h>
  29. #endif
  30.  
  31.  
  32. // ----- OpenDoc Includes -----
  33.  
  34. #ifndef _SHAPE_
  35. #include <Shape.h>
  36. #endif
  37.  
  38. #ifndef _WINDOW_
  39. #include <Window.h>
  40. #endif
  41.  
  42. #ifndef _MENUBAR_
  43. #include <MenuBar.h>
  44. #endif
  45.  
  46. #ifndef _XMPSESSM_
  47. #include <XMPSessM.h>
  48. #endif
  49.  
  50. #ifndef _DISPTCH_
  51. #include <Disptch.h>
  52. #endif
  53.  
  54. #ifndef _INFO_
  55. #include <Info.h>
  56. #endif
  57.  
  58. // ----- Textension Includes -----
  59.  
  60. #ifndef _Textension_
  61. #include "Textension.h"
  62. #endif
  63.  
  64. #ifndef _TextensionCommand_
  65. #include "TextensionCommand.h"
  66. #endif
  67.  
  68. // ----- TextPart Includes -----
  69.  
  70. #ifndef _TEXTUTIL_
  71. #include "TextUtil.h"
  72. #endif
  73.  
  74. #ifndef _TEXTFACET_
  75. #include "TextFacet.h"
  76. #endif
  77.  
  78. #ifndef _TEXTFRAME_
  79. #include "TextFrame.h"
  80. #endif
  81.  
  82. #ifndef _TEXTPART_
  83. #include "TextPart.h"
  84. #endif
  85.  
  86. #ifndef _STYLEINFO_
  87. #include "StyleInfo.h"
  88. #endif
  89.  
  90. #ifndef _ODTEXT_
  91. #include "ODText.h"
  92. #endif
  93.  
  94. #pragma segment TextPartSegment
  95.  
  96.  
  97. //========================================================================================
  98. // CLASS CTextFrame
  99. //========================================================================================
  100.  
  101. //----------------------------------------------------------------------------------------
  102. // CTextFrame::CTextFrame
  103. //----------------------------------------------------------------------------------------
  104.  
  105. CTextFrame::CTextFrame() :
  106.     FW_CEmbeddingFrame(),
  107.     fTextPart(NULL),
  108.     fRulerFrame(NULL)
  109. {
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. // CTextFrame::ITextFrame
  114. //----------------------------------------------------------------------------------------
  115.  
  116. void CTextFrame::ITextFrame(XMPFrame* xmpFrame, CTextPart* textPart)
  117. {
  118.     InitEmbeddingFrame(xmpFrame, textPart);
  119.  
  120.     // ----- By default we put all of them
  121.     AddToFocusSet(textPart->GetKeyFocusToken());
  122.     AddToFocusSet(textPart->GetMenuFocusToken());
  123.     AddToFocusSet(textPart->GetSelectionFocusToken());
  124.  
  125.     // ----- Register for idle time
  126.     textPart->GetSession()->GetDispatcher()->RegisterIdle(textPart, GetXMPFrame(), 15);
  127.  
  128.     this->SetDroppable(TRUE);
  129.  
  130.     fTextPart = textPart;
  131. }
  132.  
  133. //----------------------------------------------------------------------------------------
  134. // CTextFrame::~CTextFrame
  135. //----------------------------------------------------------------------------------------
  136.  
  137. CTextFrame::~CTextFrame()
  138. {
  139.     fTextPart->GetSession()->GetDispatcher()->UnregisterIdle(fTextPart, GetXMPFrame());
  140. }
  141.  
  142. //----------------------------------------------------------------------------------------
  143. // CTextFrame::HandleKeyDown
  144. //----------------------------------------------------------------------------------------
  145.  
  146. FW_Boolean CTextFrame::HandleKeyDown(XMPEventData event)
  147. {
  148.     static CTextensionKeyCommand* lastCommand = NULL;
  149.     
  150.     CTextDrawInitiator di((CTextFacet *) GetActiveFacet());
  151.     
  152.     CTextension* text = fTextPart->GetEditText();
  153.     
  154.     fTextPart->SetEmbeddedRunFrame(this);
  155.     fTextPart->SetEmbeddedRunFacet((CTextFacet *) GetActiveFacet());
  156.     
  157.     text->KeyDown(event);
  158.     
  159.     this->InvalidateAllFacets((CTextFacet *) GetActiveFacet(), NULL);
  160.     
  161.     GrowFrame();
  162.     
  163.     return true;
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. // CTextFrame::DoIdle
  168. //----------------------------------------------------------------------------------------
  169.  
  170. FW_Boolean CTextFrame::DoIdle()
  171. {
  172.     CTextFacet* facet = (CTextFacet *) GetActiveFacet();
  173.     
  174.     if (facet)
  175.     {
  176.         CTextDrawInitiator di(facet);
  177.         
  178.         fTextPart->SetEmbeddedRunFrame(this);
  179.         fTextPart->SetEmbeddedRunFacet((CTextFacet *) GetActiveFacet());
  180.     
  181.         (fTextPart->GetEditText())->Idle();
  182.     
  183.         return true;
  184.     }
  185.     else
  186.         return false;
  187. }
  188.  
  189. //----------------------------------------------------------------------------------------
  190. // CTextFrame::FrameShapeChanged
  191. //----------------------------------------------------------------------------------------
  192.  
  193. void CTextFrame::FrameShapeChanged()
  194. {
  195.     COpenDocText* textEdit = fTextPart->GetEditText();
  196.     XMPFrame* xmpFrame = GetXMPFrame();
  197.     
  198.     xmpFrame->Invalidate(xmpFrame->GetUsedShape());
  199.  
  200.     FW_CEmbeddingFrame::FrameShapeChanged();
  201.     
  202.     FW_CRect frameRect;
  203.     GetFrameShapeBounds(&frameRect);
  204.  
  205.     // ----- Set the new view rect size
  206.     
  207.     FW_SPlatformRect qdFrameRect = frameRect;
  208.     textEdit->SetViewRect(&qdFrameRect);
  209.             
  210.     // ----- Set the frame size. This is similar to the dest rect in TE.
  211.     
  212.     frameRect.Inset(kBorderInset, kBorderInset);    
  213.     
  214.     LongPoint frameSize(FixedToInt(frameRect.Width()), 0/*0 means unlimited hite*/);
  215.  
  216.     CFrames* frames = textEdit->GetFramesHandler();
  217.     
  218.     CDisplayChanges displayChanges;
  219.     frames->SetTextFrameSize(&frameSize, &displayChanges);
  220.     
  221.     // ------ Reflow the text if necessary.
  222.     
  223.     short action = textEdit->DisplayChanged(&displayChanges);
  224.     
  225.     // ------ Notify embedded parts of position changes.
  226.     
  227.     textEdit->NotifyEmbeddedPartsOfPositionChange(0, textEdit->CountLines() - 1);
  228. }
  229.  
  230. //----------------------------------------------------------------------------------------
  231. // CTextFrame::FocusStateChanged
  232. //----------------------------------------------------------------------------------------
  233.  
  234. void CTextFrame::FocusStateChanged(XMPTypeToken focus, FW_Boolean newState)
  235. {
  236.     FW_CEmbeddingFrame::FocusStateChanged(focus, newState);
  237.     
  238.     CTextDrawInitiator di((CTextFacet *) GetActiveFacet());
  239.  
  240.     if (focus == GetPart()->GetKeyFocusToken())
  241.     {
  242.         if (newState)
  243.         {
  244.             fTextPart->SetEmbeddedRunFrame(this);
  245.             fTextPart->SetEmbeddedRunFacet((CTextFacet *) GetActiveFacet());
  246.             
  247.             (fTextPart->GetEditText())->Activate(true, true);
  248.             ShowRuler();
  249.         }
  250.         else
  251.         {
  252.             fTextPart->SetEmbeddedRunFrame(this);
  253.             fTextPart->SetEmbeddedRunFacet((CTextFacet *) GetActiveFacet());
  254.             
  255.             (fTextPart->GetEditText())->Activate(false, false);
  256.             HideRuler();
  257.         }
  258.     }
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // CTextFrame::NewFacet
  263. //----------------------------------------------------------------------------------------
  264.  
  265. FW_CFacet* CTextFrame::NewFacet(XMPFacet* xmpFacet)
  266. {
  267.     CTextFacet* facet = new CTextFacet;
  268.     facet->InitTextFacet(xmpFacet, fTextPart);
  269.     return facet;
  270. }
  271.  
  272. //----------------------------------------------------------------------------------------
  273. // CTextFrame::DoMenuEvent
  274. //----------------------------------------------------------------------------------------
  275.  
  276. FW_Boolean CTextFrame::DoMenuEvent(XMPMenuBar *menuBar, XMPCommandID command)
  277. {
  278.     XMPMenuID menuId;
  279.     XMPMenuItemID itemId;
  280.     
  281.     CTextDrawInitiator di((CTextFacet *) GetActiveFacet());
  282.     
  283.     menuBar->GetMenuAndItem(command, menuId, itemId);
  284.     MenuHandle menu = menuBar->GetMenu(menuId);
  285.  
  286.     switch (menuId)
  287.     {
  288.         case CTextPart::kSizeMenu:
  289.             fTextPart->SetEmbeddedRunFrame(this);
  290.             fTextPart->SetEmbeddedRunFacet((CTextFacet *) GetActiveFacet());
  291.             fTextPart->SetFontSize(gFontSizeTable[itemId - 1].fFontSize);
  292.             break;
  293.         
  294.         case CTextPart::kFontMenu:
  295.             {
  296.                 Str255 fontName;
  297.                 
  298.                 ::GetItem(menu, itemId, fontName);
  299.                 fTextPart->SetEmbeddedRunFrame(this);
  300.                 fTextPart->SetEmbeddedRunFacet((CTextFacet *) GetActiveFacet());
  301.                 fTextPart->SetFont(fontName);
  302.             }
  303.             break;
  304.         
  305.         default:
  306.             return FW_CEmbeddingFrame::DoMenuEvent(menuBar, command);
  307.     }
  308.         
  309.     return true;
  310. }
  311.  
  312. //----------------------------------------------------------------------------------------
  313. // CTextFrame::FacetAdded
  314. //----------------------------------------------------------------------------------------
  315.  
  316. void CTextFrame::FacetAdded(FW_CFacet* facet)
  317. {
  318.     FW_CEmbeddingFrame::FacetAdded(facet);
  319. }
  320.  
  321. //----------------------------------------------------------------------------------------
  322. // CTextFrame::FacetRemoved
  323. //----------------------------------------------------------------------------------------
  324.  
  325. void CTextFrame::FacetRemoved(FW_CFacet* facet)
  326. {
  327.     FW_CEmbeddingFrame::FacetRemoved(facet);
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. // CTextFrame::ShowRuler
  332. //----------------------------------------------------------------------------------------
  333.  
  334. void CTextFrame::ShowRuler()
  335. {
  336. #ifdef NOT_WORKING
  337.     if (this->GetXMPFrame()->IsRoot())
  338.         return;
  339.         
  340.     XMPFrame* rootFrame = GetActiveFacet()->GetXMPWindow()->GetRootFrame();
  341.     XMPFacet* rootFacet = GetActiveFacet()->GetXMPWindow()->GetRootFacet();
  342.     XMPPart* rootPart = rootFrame->GetPart();
  343.     
  344.     FW_CRect rect;
  345.     rootFrame->GetFrameShape()->GetBoundingBox(&rect);
  346.     rect.right = IntToFixed(FixedToInt(rect.right) - 15);
  347.     rect.bottom = IntToFixed(FixedToInt(rect.top)
  348.                     + CTextPart::kRulerRulerHeight
  349.                     + CTextPart::kRulerToolsHeight);
  350.     
  351.     XMPShape* newFrameShape = ::NewXMPShape(rect);
  352.  
  353.     fRulerFrame = rootPart->GetStorageUnit()->GetDraft()->
  354.             CreateFrame(rootFrame, newFrameShape, fTextPart,
  355.                         fTextPart->GetSession()->Tokenize(kXMPViewAsFrame),
  356.                         fTextPart->GetRulerPresentation(),
  357.                         false, true);
  358.                         
  359.     fRulerFrame = rootPart->RequestEmbeddedFrame(rootFrame,
  360.                                                 this->GetXMPFrame(),
  361.                                                  newFrameShape,
  362.                                                  fTextPart,
  363.                                                  fTextPart->GetSession()->Tokenize(kXMPViewAsFrame),
  364.                                                  fTextPart->GetRulerPresentation(),
  365.                                                  true);                                                
  366.     
  367.     XMPShape* facetClip = ::NewXMPShape(fRulerFrame->GetFrameShape());
  368.     FW_SPlatformPoint pt = { 0, 0 };
  369.     XMPTransform* facetTransform = ::NewXMPTransform(pt);
  370.     XMPFacet* rulerFacet
  371.         = rootFacet->CreateEmbeddedFacet(fRulerFrame,
  372.                                          facetClip,
  373.                                          facetTransform,
  374.                                          NULL, kXMPFrameInFront);
  375.     fRulerFrame->Invalidate(NULL);
  376. #endif
  377. }
  378.  
  379. //----------------------------------------------------------------------------------------
  380. // CTextFrame::HideRuler
  381. //----------------------------------------------------------------------------------------
  382.  
  383. void CTextFrame::HideRuler()
  384. {
  385. #ifdef NOT_WORKING
  386.     if (this->GetXMPFrame()->IsRoot())
  387.         return;
  388.         
  389.     fRulerFrame->Invalidate(NULL);
  390.  
  391.     XMPFrame* rootFrame = GetActiveFacet()->GetXMPWindow()->GetRootFrame();
  392.     XMPFacet* rootFacet = GetActiveFacet()->GetXMPWindow()->GetRootFacet();
  393.     XMPPart* rootPart = rootFrame->GetPart();
  394.     
  395.     XMPFacet* facet;
  396.     BC_TDynamicCollection<XMPFacet *, BC_CUnmanaged> facets(4);
  397.     
  398.     XMPFrameFacetIterator it1(fRulerFrame);    
  399.     for (facet = it1.First(); it1.IsNotComplete(); facet = it1.Next())
  400.         facets.Append(facet);
  401.     
  402.     BC_TCollectionActiveIterator<XMPFacet *> it2(facets);
  403.     for (; !it2.IsDone(); it2.Next())
  404.     {
  405.         rootFacet->RemoveFacet(facet);
  406.         delete facet;
  407.     }
  408.  
  409.     rootPart->RemoveEmbeddedFrame(fRulerFrame);
  410.     
  411.     fRulerFrame = NULL;
  412. #endif
  413. }
  414.  
  415. //----------------------------------------------------------------------------------------
  416. // CTextFrame::GrowFrame
  417. //----------------------------------------------------------------------------------------
  418.  
  419. void CTextFrame::GrowFrame()
  420. {
  421.     CTextension* textRecord = fTextPart->GetEditText();
  422.     long textHeight  = textRecord->GetTotalLinesHite();
  423.     
  424.     FW_CRect frameRect;
  425.     GetFrameShapeBounds(&frameRect);
  426.     
  427.     if (textHeight > FixedToInt(frameRect.Height()) - 2 * kBorderInset)
  428.     {
  429.         // Calculate the new frame size and request the change.
  430.         
  431.         XMPShape* shape = new XMPShape();
  432.         
  433.         frameRect.bottom 
  434.             = ff(FixedToInt(frameRect.top) + kBorderInset + textHeight + kBorderInset);
  435.         shape->SetRectangle(&frameRect);
  436.         RequestFrameShape(shape);
  437.         UpdateUsedAndActiveShapes();
  438.         
  439.         // Invalidate the entire frame. Should be smarter about this.
  440.         
  441.         Invalidate(shape);
  442.         
  443.         // ----- Set the new view rect size
  444.         
  445.         FW_SPlatformRect qdFrameRect;
  446.         frameRect.AsPlatformRect(qdFrameRect);
  447.         textRecord->SetViewRect(&qdFrameRect);
  448.                 
  449.         // ----- Set the frame size. This is similar to the dest rect in TE.
  450.         
  451.         frameRect.Inset(kBorderInset, kBorderInset);    
  452.         
  453.         LongPoint frameSize(FixedToInt(frameRect.Width()), 0/*0 means unlimited hite*/);
  454.     
  455.         CFrames* frames = textRecord->GetFramesHandler();
  456.         
  457.         CDisplayChanges displayChanges;
  458.         frames->SetTextFrameSize(&frameSize, &displayChanges);
  459.     }
  460. }
  461.  
  462.